package mb.utils;
import java.util.concurrent.*;
import java.io.*;

public class LogText implements LogInterface, Runnable{
    public static final int LOG_LEVEL_FULL = 0;
    public static final int LOG_LEVEL_CRITICAL = 1;
    public static final int LOG_LEVEL_ERROR = 2;
    public static final int LOG_LEVEL_DEBUG = 3;
    
    BlockingQueue<String> queue = new LinkedBlockingQueue<String>();
    boolean value = false;
    PrintWriter pw;
    boolean isKeepRunning = true;
    String sLogFileName;
    boolean isLogDateTime = true;
    private int iLogLevel;
	
    public LogText(String sLogFileName,int iLogLevel,boolean bPseudo) {
        try{
            this.sLogFileName  = sLogFileName;
            this.iLogLevel = iLogLevel;
            openLog();
        }
        catch(Exception ex){}
    }
    
    public LogText(String sLogFileName) throws Exception{
        this.sLogFileName  = sLogFileName;
        this.iLogLevel = LOG_LEVEL_FULL;
        openLog();
    }
    public LogText(String sLogFileName, int iLogLevel) throws Exception{
        this.sLogFileName  = sLogFileName;
        this.iLogLevel = iLogLevel;
        openLog();
    }
    public void setLogWithDateTime(boolean isLogDateTime){
        this.isLogDateTime = isLogDateTime;
    }

    public void log(String sData){
        try{
            queue.put(sData);
        }
        catch(Exception ex){

        }		
    }
    public void log(String sData, int iLogLevel){
        try{
            if(this.getiLogLevel() >= iLogLevel){
                queue.put(sData);
            }
        }
        catch(Exception ex){

        }		
    }
    public int getQueueSize(){
        return queue.size();
    }

    public void openLog() throws Exception{
        String fileSeparator = System.getProperty("file.separator");
        String logDirectory = System.getProperty("user.dir") + fileSeparator + "logs" + fileSeparator + Utils.getFileNamewithDateTime();
        //create directory for log first
        boolean success = (new File(logDirectory)).mkdir();
        String logFile = logDirectory + fileSeparator + sLogFileName;
        try{
                pw = new PrintWriter(new FileWriter(new File(logFile + ".txt")), true); 
        }
        catch(Exception ex){
            throw new Exception("Error opening log file " + logFile);
        }
    }
    public void closeLog(){
        try{
            isKeepRunning = false;
            //finish logging 
            if(pw != null) {
                    pw.close();
                    pw = null;
            }
        }
        catch(Exception ex){}	
    }
    /*
    public void logObject(TSCBaseObjectData logData){
            System.out.println("Log text- Log object not implemented");
    }
    */	
    public void run(){
        //get the log linked list and write to log file
        while(isKeepRunning){
            try{
                String logString =queue.take();
                if((pw!= null) && (logString!= null)) {
                        if(isLogDateTime){
                                pw.println(Utils.getCurrentDateTime() +  "(Milis=" + System.currentTimeMillis() +  "):"  + logString);
                        }
                        else{
                                pw.println(logString);
                        }					
                        pw.flush();
                }
            }
            catch(Exception ex){
                    closeLog();
            }			
        }
    }

    /**
     * @return the iLogLevel
     */
    public int getiLogLevel() {
        return iLogLevel;
    }

    /**
     * @param iLogLevel the iLogLevel to set
     */
    public void setiLogLevel(int iLogLevel) {
        this.iLogLevel = iLogLevel;
    }
    
}